/**
 * 基于每周的时间切换
 * @author sunyu 2014-10-26
 * 使用说明：在需要的页面引入dayChange.css
 * 然后在需要引入插件的地方插入<div class="dayChange"></div>
 * 例如：
 *  <div class="pre_next">
 *       <div class="dayChange"></div>
 *  </div>
 * example :
 * $(".dayChange").weekChange({
 * 		startTime:0,//可指定从1970到现在毫秒数来初始化插件
 *		call_back:loadData		
 *	});
 */
;(function(factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as an anonymous module.
		define([ 'jquery.weekChange' ], factory);
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function($) {
    $.fn.weekChange = function(opts) {
        opts = jQuery.extend({
        	startTime:0,
            call_back: function() {
                return false
            }
        },
        opts || {});
        return this.each(function() {
			var panel = jQuery(this);
			var html = "<a href='javascript:void(0)' id='preDay'><span id='left_dire'>Pre</span></a><span id='date_text'></span><a href='javascript:void(0)' id='nextDay'><span id='right_dire'>Next</span></a>";
            panel.html(html);//附加html
            var flag = opts.setMax;//如果选择设置最大时间则，将最大时间设为今天
            //如过没有给插件指定开始时间则自动生成当前时间作为开始时间
            var start = parseInt(opts.startTime);
            var millis;
            if(start==0){
            	var date = new Date(); //获得时间对象
            	millis = date.getTime();//获得毫秒数
            }else{
            	millis = start;
            }
            writeDate(millis);//写入显示日期
			//回调
            function dayClick(time) {
                opts.call_back(time);
            }
            //得到日期是一年中的第几周
            function getWeekNumber(d) {
	            // Copy date so don't modify original
	            d = new Date(d);
	            d.setHours(0,0,0);
	            // Set to nearest Thursday: current date + 4 - current day number
	            // Make Sunday's day number 7
	            d.setDate(d.getDate() + 4 - (d.getDay()||7));
	            // Get first day of year
	            var yearStart = new Date(d.getFullYear(),0,1);
	            // Calculate full weeks to nearest Thursday
	            var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
	            // Return array of year and week number
	            var getFullYear = d.getFullYear();
	            var getWeekNo = weekNo;
	            return [getFullYear,getWeekNo];
            }
            //写入日期
            function writeDate(time) {
               var arr=getWeekNumber(time);
				$("#date_text").html(arr[0]+"年 第"+arr[1]+"周");	
            } 
            //点击上一天
            $("#preDay").click(function() {
            		millis = millis - 604800000;//一周毫秒数
                    writeDate(millis); //写入日期
                    dayClick(millis);//调用回调(传入毫秒数)
                    return false;
            });
            //点击下一天
            $("#nextDay").click(function() {
            		millis = millis + 604800000;
                    writeDate(millis); //写入日期
    				dayClick(millis);//调用回调(传入毫秒数)
                    return false;
           });
        });
    }
}));